### 处理meteonorm的dat文件，并依据meteonorm文件的数据计算光照小时数:
# todo: 功能尚不全，临时写了一个用来获得年总ghi的
import requests
import pandas as pd
import os

columns = ['month', 'day', 'hour', 'index', 'GHI', 'DHI', 'DNI', 'TEMP', 'TD', 'FF']
pro_name = '重庆市'
data_path = f'..\\file\meteonorm_data\\{pro_name}'
prolist,citylist,countylist,lonlist,latlist = [],[],[],[],[]
ghi_year_list = []
res ={'province': prolist, 'city': citylist, 'county': countylist, 'lon':lonlist, 'lat':latlist, 'ghi_year': ghi_year_list}
for filaneme in os.listdir(data_path):
    if not filaneme.endswith('.dat'):
        continue
    info = filaneme.split('_')
    province,city,county = info[0], info[1], info[2]
    lon = float(info[3]+"."+info[4]).__round__(4)
    lat = float(info[5]+"."+info[6].split('-')[0]).__round__(4)
    df = pd.read_csv(os.path.join(data_path, filaneme), sep='\s+', header=None, names=columns, encoding='utf-8')
    df['year'] = 2025
    df['datetime'] = pd.to_datetime(df[['year', 'month', 'day', 'hour']], format='%Y-%m-%d %H')
    ghi_year = df['GHI'].sum()/1000
    prolist.append(province)
    citylist.append(city)
    countylist.append(county)
    lonlist.append(lon)
    latlist.append(lat)
    ghi_year_list.append(ghi_year)
res = pd.DataFrame(res)
# 同时与之匹配面积信息：
gridpath = r'D:\work_files\projcet\generation_hour\phour_easy_report\网格化采样点\各省区级采样点'
filename = f'{pro_name}网格化结果.xlsx'
df = pd.read_excel(os.path.join(gridpath, filename))
df['lon'] = df['lon'].apply(lambda x: round(x, 4))
df['lat'] = df['lat'].apply(lambda x: round(x, 4))
res = pd.merge(res, df, on=['lon', 'lat'], how='left')
res = res[['province', 'city', 'county', 'lon', 'lat', '面积', 'ghi_year']]
res.to_excel(f'../file/meteonorm2excel/{pro_name}_meteonorm.xlsx', index=False)




